package com.lxm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxm.common.contants.AdminConstants;
import com.lxm.common.tools.UserHolder;
import com.lxm.dto.AdminDTO;
import com.lxm.dto.PhonePwdLoginDTO;
import com.lxm.dto.Result;
import com.lxm.entity.Admin;
import com.lxm.mapper.AdminDao;
import com.lxm.service.AdminService;
import com.lxm.service.UtilsService;
import com.lxm.vo.LoginVO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2024-10-02
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements AdminService {

    @Resource
    private UtilsService utilsService;

    @Override
    public Result addAdmin(AdminDTO adminDTO) {
        LoginVO user = UserHolder.getUser();
        Integer isAdmin = user.getIsAdmin();
        Long userId = user.getUserId();
        if (exists(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getPhone, adminDTO.getPhone()))) return Result
                .fail(AdminConstants.ADMIN_PHONE_REGISTER_EXIST);
        if (exists(new LambdaQueryWrapper<Admin>()
                .eq(Admin::getEmail, adminDTO.getEmail()))) return Result
                .fail(AdminConstants.ADMIN_EMAIL_REGISTER_EXIST);
        if (isAdmin == 0) return Result.fail(AdminConstants.NOT_ADMIN_FAIL);
        Boolean isHighestAdmin = getIsHighestAdmin(userId);
        if (!isHighestAdmin) return Result.fail(AdminConstants.NOT_HIGHEST_ADMIN_FAIL);
        Admin admin = BeanUtil.toBean(adminDTO, Admin.class);
        boolean save = save(admin);
        if (!save) return Result.fail(AdminConstants.ADD_ADMIN_FAIL);
        return Result.ok();
    }


    @Override
    public Result adminLogin(PhonePwdLoginDTO login, HttpServletResponse response) {
        Admin admin = lambdaQuery()
                .eq(Admin::getPhone, login.getPhone())
                .eq(Admin::getAdminPwd, login.getPwd())
                .select(Admin::getAdminId, Admin::getAdminName)
                .one();
        if (BeanUtil.isEmpty(admin)) return Result.fail(AdminConstants
                .PHONE_PASSWORD_LOGIN_ERROR);
        LoginVO loginVO = new LoginVO();
        loginVO.setUserId(admin.getAdminId());
        loginVO.setNickName(admin.getAdminName());
        loginVO.setIsAdmin(1);
        utilsService.TWTToken(response, loginVO);
        return Result.ok(AdminConstants.PHONE_PASSWORD_LOGIN_SUCCESS);
    }

    @Override
    public Result getAdminList(Long departmentId) {
        LoginVO user = UserHolder.getUser();
        Integer isAdmin = user.getIsAdmin();
        if (isAdmin == 0) return Result.fail(AdminConstants.NOT_ADMIN_FAIL);
        Boolean isHighestAdmin = getIsHighestAdmin(user.getUserId());
        if (!isHighestAdmin) return Result.fail(AdminConstants.NOT_HIGHEST_ADMIN_FAIL);
        List<Admin> adminList = lambdaQuery()
                .eq(Admin::getDeptId, departmentId)
                .list();
        //这里交由前端判断是否部门有人
        return Result.ok(adminList);
    }

    @Override
    public Result deleteAdmin(Long id) {
        LoginVO user = UserHolder.getUser();
        Integer isAdmin = user.getIsAdmin();
        if (isAdmin == 0) return Result.fail(AdminConstants.NOT_ADMIN_FAIL);
        Boolean isHighestAdmin = getIsHighestAdmin(user.getUserId());
        if (!isHighestAdmin) return Result.fail(AdminConstants.NOT_HIGHEST_ADMIN_FAIL);
        boolean removeById = removeById(id);
        return removeById ? Result.ok() : Result.fail(AdminConstants.DELETE_ADMIN_FAIL);
    }

    @Nullable
    private Boolean getIsHighestAdmin(Long userId) {
        Integer deptId = lambdaQuery()
                .eq(Admin::getAdminId, userId)
                .select(Admin::getDeptId)
                .one().getDeptId();
        return deptId == 1;
    }
}
